module.exports = {
  // --- 数组操作 ---
  // 数组合并
  arrayConcat() {
    var tmpArr = [];
    for (let i = 0; i < arguments.length; i++) {
      tmpArr = tmpArr.concat(arguments[i]);
    }
    return tmpArr;
  },
  arrayDrop(array, index, howmany) {
    if (!index) {
      index = 0;
    }
    if (!howmany) {
      howmany = 1;
    }
    array.splice(index, howmany);
    return array;
  },
  arrayIndexOf(arr, needFind) {
    var index = -1;
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] == needFind) {
        index = i;
        return i;
      }
    }
    return index;
  },
  arrayDifference(a, b) {
    const set = new Set(b);
    return a.filter(x => !set.has(x));
  },
  arrayShuffle(arr) {
    let l = arr.length;
    while (l) {
      const i = Math.floor(Math.random() * l--);
      [arr[l], arr[i]] = [arr[i], arr[l]];
    }
    return arr;
  },
  arraySum(arr) {
    return arr.reduce((acc, val) => acc + val, 0);
  },
  arrayAvg(arr) {
    return arr.reduce((acc, val) => acc + val, 0) / arr.length;
  },
  arrayEach(arr, fun) {
    for (let i = 0; i < arr.length; i++) {
      fun(arr[i], i);
    }
  },

  // 2数之间的随机数
  random(min, max) {
    switch (arguments.length) {
      case 1:
        return parseInt(Math.random() * min + 1, 10);
        break;
      case 2:
        return parseInt(Math.random() * (max - min + 1) + min, 10);
        break;
      default:
        return 0;
    }
  },

  // UUID
  uuid(len) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [],
      i;
    if (len) {
      for (i = 0; i < len; i++) {
        uuid[i] = chars[0 | Math.random() * chars.length];
      }
    } else {
      var r;
      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
      uuid[14] = '4';
      for (i = 0; i < 36; i++) {
        if (!uuid[i]) {
          r = 0 | Math.random() * 16;
          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
        }
      }
    }
    return uuid.join('');
  },
}
